﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MongoDB.Bson;
using MongoDB.Driver;
using ValuationResource;
using ValuationLogic;
using System.IO;
using System.Text;
namespace ValuationModel
{
    public class actions
    {
        UserModel userModel = new UserModel();
    
        public bool addSwap(Swap newSwap,string userName)
        {
            BsonDocument doc = new BsonDocument();
            doc["swapId"] = newSwap.getSwapId();
            doc["userId"] = newSwap.getUserId();
            doc["counterparty"] = newSwap.getCounterparty();
            doc["commodity"] = newSwap.getCommodity();
            doc["currencyUnit"] = newSwap.getCurrencyUnit();
            doc["commodityUnit"] = newSwap.getCommodityUnit();
            doc["quoteCode"] = newSwap.getQuoteCode();
            doc["settlementSpec"] = newSwap.getSettlementSpec();
            doc["buySwap"] = newSwap.isBuySwap();
            doc["quantity"] = newSwap.getQuantity();
            doc["fixedPrice"] = newSwap.getFixedPrice();
            doc["risk"] = newSwap.getRiskExposure();
            doc["dealDate"] = newSwap.getDealDate();
            doc["periodStartDate"] = newSwap.getPeriodStartDate();
            doc["periodEndDate"] = newSwap.getPeriodEndDate();
            doc["calDate"] = newSwap.getCalDate();
            doc["PnL"] = newSwap.getPnL();

            string myPath = @"c:\" + userName + ".txt";
            StreamWriter sw = new StreamWriter(myPath, true);
            sw.WriteLine("<?MoganProject version=\"1.0\"?>");
            sw.WriteLine("#Swap");
            sw.WriteLine("#swapId {0}",newSwap.getSwapId());
            sw.WriteLine("#userId {0}", newSwap.getUserId());
            sw.WriteLine("#counterparty {0}",newSwap.getCounterparty());
            sw.WriteLine("#commodity {0}",newSwap.getCommodity());
            sw.WriteLine("#currencyUnit {0}",newSwap.getCurrencyUnit());
            sw.WriteLine("#commodityUnit {0}",newSwap.getCommodityUnit());
            sw.WriteLine("#quoteCode {0}",newSwap.getQuoteCode());
            sw.WriteLine("#settlementSpec {0}",newSwap.getSettlementSpec());
            sw.WriteLine("#buySwap {0}",newSwap.isBuySwap());
            sw.WriteLine("#quantity {0}",newSwap.getQuantity());
            sw.WriteLine("#fixedPrice {0}",newSwap.getFixedPrice());
            sw.WriteLine("#risk {0}",newSwap.getRiskExposure());
            sw.WriteLine("#dealDate {0}",newSwap.getDealDate());
            sw.WriteLine("#periodStartDate {0}", newSwap.getPeriodStartDate());
            sw.WriteLine("#periodEndDate {0}",newSwap.getPeriodEndDate());
            sw.WriteLine("#PnL {0}", newSwap.getPnL());
            sw.WriteLine("#EndSwap");
            sw.Close();
            try
            {
                userModel.Add(doc);
                return true;
            }
            catch
            {
                return false;
            }
        }
        public void updateSwap(List<Swap> swapList, string userName)
        {
            string myPath2 = @"c:\" + userName + ".txt";
            FileStream fs = File.Open(myPath2, FileMode.Truncate, FileAccess.ReadWrite, FileShare.Read);//清空后写入
            fs.Close();
            StreamWriter sw = new StreamWriter(myPath2, true);
            for (int i = 0; i < swapList.Count; i++)
            {
                sw.WriteLine("<?MoganProject version=\"1.0\"?>");
                sw.WriteLine("#Swap");
                sw.WriteLine("#swapId {0}", swapList[i].getSwapId());
                sw.WriteLine("#userId {0}", swapList[i].getUserId());
                sw.WriteLine("#counterparty {0}", swapList[i].getCounterparty());
                sw.WriteLine("#commodity {0}", swapList[i].getCommodity());
                sw.WriteLine("#currencyUnit {0}", swapList[i].getCurrencyUnit());
                sw.WriteLine("#commodityUnit {0}", swapList[i].getCommodityUnit());
                sw.WriteLine("#quoteCode {0}", swapList[i].getQuoteCode());
                sw.WriteLine("#settlementSpec {0}", swapList[i].getSettlementSpec());
                sw.WriteLine("#buySwap {0}", swapList[i].isBuySwap());
                sw.WriteLine("#quantity {0}", swapList[i].getQuantity());
                sw.WriteLine("#fixedPrice {0}", swapList[i].getFixedPrice());
                sw.WriteLine("#risk {0}", swapList[i].getRiskExposure());
                sw.WriteLine("#dealDate {0}", swapList[i].getDealDate());
                sw.WriteLine("#periodStartDate {0}", swapList[i].getPeriodStartDate());
                sw.WriteLine("#periodEndDate {0}", swapList[i].getPeriodEndDate());
                sw.WriteLine("#PnL {0}", swapList[i].getPnL());
                sw.WriteLine("#EndSwap");
            }
            sw.Close();
        }
 
        public bool updateSwap(Swap newSwap)
        {
            BsonDocument doc = new BsonDocument();
            doc["swapId"] = newSwap.getSwapId();
            doc["userId"] = newSwap.getUserId();
            doc["counterparty"] = newSwap.getCounterparty();
            doc["commodity"] = newSwap.getCommodity();
            doc["currencyUnit"] = newSwap.getCurrencyUnit();
            doc["commodityUnit"] = newSwap.getCommodityUnit();
            doc["quoteCode"] = newSwap.getQuoteCode();
            doc["settlementSpec"] = newSwap.getSettlementSpec();
            doc["buySwap"] = newSwap.isBuySwap();
            doc["quantity"] = newSwap.getQuantity();
            doc["fixedPrice"] = newSwap.getFixedPrice();
            doc["risk"] = newSwap.getRiskExposure();
            doc["PnL"] = newSwap.getPnL();
            doc["dealDate"] = newSwap.getDealDate();
            doc["periodStartDate"] = newSwap.getPeriodStartDate();
            doc["periodEndDate"] = newSwap.getPeriodEndDate();
            doc["calDate"] = newSwap.getCalDate();
            try
            {
                userModel.Update(doc);
                return true;
            }
            catch
            {
                return false;
            }
        }

        public void selectAllSwap(string userId)
        {
            userModel.selectAll(userId);
        }
        public List<Swap> getSwaps(User user)
        {
            List<Swap> myList = new List<Swap>();
            
            string fileName = "c:\\"+ user.name + ".txt";
            StreamReader sr = new StreamReader(fileName, Encoding.Default);
            string nn;
          
            while ((nn = sr.ReadLine()) != null)
          {
              if (nn.StartsWith("#Swap"))
              {
                  Swap mySwap = new Swap();
                  while ((nn = sr.ReadLine()) != "#EndSwap")
                  {
                      if (nn.StartsWith("#swapId"))
                      {
                          char[] delimiterChars = { ' ' };
                          string[] words = nn.Split(delimiterChars);
                          string n = null;
                          for (int i = 1; i < words.Length; i++)
                              n += words[i];
                          mySwap.setSwapId(n);
                      }
                      else if (nn.StartsWith("#userId"))
                      {
                          char[] delimiterChars = { ' ' };
                          string[] words = nn.Split(delimiterChars);
                          string n = null;
                          for (int i = 1; i < words.Length; i++)
                              n += words[i];
                          mySwap.setUserId(n);
                      }
                      else if (nn.StartsWith("#counterparty"))
                      {
                          char[] delimiterChars = { ' ' };
                          string[] words = nn.Split(delimiterChars);
                          string n = null;
                          for (int i = 1; i < words.Length; i++)
                              n += words[i];
                          mySwap.setCounterparty(n);
                      }
                      else if (nn.StartsWith("#commodity"))
                      {
                          char[] delimiterChars = { ' ' };
                          string[] words = nn.Split(delimiterChars);
                          string n = null;
                          for (int i = 1; i < words.Length; i++)
                              n += words[i];
                          mySwap.setCommodity(n);
                      }
                      else if (nn.StartsWith("#currencyUnit"))
                      {
                          char[] delimiterChars = { ' ' };
                          string[] words = nn.Split(delimiterChars);
                          string n = null;
                          for (int i = 1; i < words.Length; i++)
                              n += words[i];
                          mySwap.setCurrencyUnit(n);
                      }
                      else if (nn.StartsWith("#commodityUnit"))
                      {
                          char[] delimiterChars = { ' ' };
                          string[] words = nn.Split(delimiterChars);
                          string n = null;
                          for (int i = 1; i < words.Length; i++)
                              n += words[i];
                          mySwap.setCurrencyUnit(n);
                      }
                      else if (nn.StartsWith("#quoteCode"))
                      {
                          char[] delimiterChars = { ' ' };
                          string[] words = nn.Split(delimiterChars);
                          string n = null;
                          for (int i = 1; i < words.Length; i++)
                              n += words[i];
                          mySwap.setQuoteCode(n);
                      }
                      else if (nn.StartsWith("#settlementSpec"))
                      {
                          char[] delimiterChars = { ' ' };
                          string[] words = nn.Split(delimiterChars);
                          string n = null;
                          for (int i = 1; i < words.Length; i++)
                              n += words[i];
                          mySwap.setSettlementSpecVal(n);
                      }
                      else if (nn.StartsWith("#buySwap"))
                      {
                          char[] delimiterChars = { ' ' };
                          string[] words = nn.Split(delimiterChars);
                          string n = null;
                          for (int i = 1; i < words.Length; i++)
                              n += words[i];
                          bool myBuySwap;
                          if (n == "1")
                              myBuySwap = true;
                          else
                              myBuySwap = false;
                          mySwap.setBuySwap(myBuySwap);
                      }
                      else if (nn.StartsWith("#quantity"))
                      {
                          char[] delimiterChars = { ' ' };
                          string[] words = nn.Split(delimiterChars);
                          string n = null;
                          for (int i = 1; i < words.Length; i++)
                              n += words[i];
                          double num;
                          num = System.Convert.ToDouble(n);
                          mySwap.setQuantity(num);
                      }
                      else if (nn.StartsWith("#fixedPrice"))
                      {
                          char[] delimiterChars = { ' ' };
                          string[] words = nn.Split(delimiterChars);
                          string n = null;
                          for (int i = 1; i < words.Length; i++)
                              n += words[i];
                          double num;
                          num = System.Convert.ToDouble(n);
                          mySwap.setFixedPrice(num);
                      }
                      else if (nn.StartsWith("#risk"))
                      {
                          char[] delimiterChars = { ' ' };
                          string[] words = nn.Split(delimiterChars);
                          string n = null;
                          for (int i = 1; i < words.Length; i++)
                              n += words[i];
                          double num;
                          num = System.Convert.ToDouble(n);
                          mySwap.setRisk(num);
                      }
                      else if (nn.StartsWith("#dealDate"))
                      {
                          //char[] delimiterChars = { ' ' };
                          //string[] words = nn.Split(delimiterChars);
                          //string n = null;
                          //for (int i = 1; i < words.Length; i++)
                          //    n += words[i];

                          int flag = nn.IndexOf(" ");
                          string n = nn.Substring(flag + 1, nn.Length - flag - 1);
                          DateTime dt = System.Convert.ToDateTime(n);
                          mySwap.setDealDate(dt);
                      }
                      else if (nn.StartsWith("#periodStartDate"))
                      {
                          //char[] delimiterChars = { ' ' };
                          //string[] words = nn.Split(delimiterChars);
                          //string n = null;
                          //for (int i = 1; i < words.Length; i++)
                          //    n += words[i];
                          int flag = nn.IndexOf(" ");
                          string n = nn.Substring(flag + 1, nn.Length - flag - 1);
                          DateTime dt = System.Convert.ToDateTime(n);
                          mySwap.setPeriodStartDate(dt);
                      }
                      else if (nn.StartsWith("#periodEndDate"))
                      {
                          //char[] delimiterChars = { ' ' };
                          //string[] words = nn.Split(delimiterChars);
                          //string n = null;
                          //for (int i = 1; i < words.Length; i++)
                          //    n += words[i];
                          int flag = nn.IndexOf(" ");
                          string n = nn.Substring(flag + 1, nn.Length - flag - 1);
                          DateTime dt = System.Convert.ToDateTime(n);
                          mySwap.setPeriodEndDate(dt);
                      }
                      else if (nn.StartsWith("#PnL"))
                      {
                          char[] delimiterChars = { ' ' };
                          string[] words = nn.Split(delimiterChars);
                          string n = null;
                          for (int i = 1; i < words.Length; i++)
                              n += words[i];
                          double num;
                          num = System.Convert.ToDouble(n);
                          mySwap.setPnL(num);
                      }
                  }
                   myList.Add(mySwap);
                }
                else
                {
                    
                }
         }
                return myList;
        }
    }
}
